#include <iostream>
#include <pthread.h>
/*饿汉模式*/
// class SingleTon
// {
// private:
//     SingleTon() {}
//     ~SingleTon() {}
//     static SingleTon _ston;
// public:
//     SingleTon(const SingleTon&) = delete;
//     SingleTon operator=(const SingleTon&) = delete;

//     static SingleTon& GetInstance()
//     {
//         return _ston;
//     }

//     void GetData()
//     {
//         std::cout << "Only one instance" << std::endl;
//     }
// };
// SingleTon SingleTon::_ston;

/*懒汉模式*/
class SingleTon
{
private:
    SingleTon() {}
    ~SingleTon() {}
public:
    SingleTon(const SingleTon&) = delete;
    SingleTon operator=(const SingleTon&) = delete;

    static SingleTon& GetInstance()
    {
        // 静态变量的初始化是线程安全的，且只会被初始化一次
        static SingleTon ston;
        return ston;
    }

    void GetData()
    {
        std::cout << "Only one instance" << std::endl;
    }
};

void* threadRoute(void* args)
{
    std::cout << "thread: " << &SingleTon::GetInstance() << std::endl;
    return nullptr;
} 

int main()
{
    pthread_t th;
    SingleTon::GetInstance().GetData();
    std::cout << "main thread: " << &SingleTon::GetInstance() << std::endl;
    pthread_create(&th, nullptr, threadRoute, nullptr);

    pthread_join(th, nullptr);
    return 0;
}